<template>
  <el-row :gutter="20">
    <!-- 左侧树 -->
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <DatabaseTree v-model="queryParams.dataSourceConfigId" @node-click="handleDeptNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="表名称" prop="tableName">
            <el-input
              v-model="queryParams.tableName"
              class="!w-240px"
              clearable
              placeholder="请输入表名称"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="表描述" prop="tableComment">
            <el-input
              v-model="queryParams.tableComment"
              class="!w-240px"
              clearable
              placeholder="请输入表描述"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="queryParams.createTime"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-240px"
              end-placeholder="结束日期"
              start-placeholder="开始日期"
              type="daterange"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
            <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
            <!-- <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['sys:databasesharing:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> 新增
            </el-button> -->
            <el-button
              v-hasPermi="['sys:databasesharing:create']"
              type="primary"
              @click="openImportTable()">
              <Icon class="mr-5px" icon="ep:zoom-in" />
              导入
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <!-- <el-table-column label="主键" align="center" prop="id" /> -->
          <el-table-column label="#" align="center" type="index" width="50" fixed="left" />
          <el-table-column label="表名称" align="center" prop="tableName" />
          <el-table-column label="表描述" align="center" prop="tableComment"/>
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180px"
          />
          <el-table-column
            label="更新时间"
            align="center"
            prop="updateTime"
            :formatter="dateFormatter"
            width="180px"
          />
          <el-table-column label="操作" align="center" min-width="240px" fixed="right">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="openDetail(scope.row)"
                v-hasPermi="['sys:databasesharing:query']"
              >
                详情
              </el-button>
              <el-button
                link
                type="primary"
                @click="openForm('update', scope.row.id)"
                v-hasPermi="['sys:databasesharing:update']"
              >
                编辑
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['sys:databasesharing:delete']"
              >
                删除
              </el-button>
              <!-- <el-button
                link
                type="primary"
                @click="handleLook(scope.row.id)"
                v-hasPermi="['sys:databasesharing:query']"
              >
                字段
              </el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>

  <!-- 表单弹窗：添加/修改 -->
  <SourceForm ref="formRef" @success="getList" />
  <!-- 详情 -->
  <SourceDetail v-model:visible="detailVisible" :data="currentRow" />
  <!-- 弹窗：导入表 -->
  <ImportTable ref="importRef" @success="getList" />
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { dateFormatter } from '@/utils/formatTime'
import * as CodegenApi from '@/api/infra/codegen'
import { CodegenTableVO } from '@/api/infra/codegen'
import SourceForm from './SourceForm.vue'
import SourceDetail from './SourceDetail.vue'
import ImportTable from '@/views/infra/codegen/ImportTable.vue'

/** 数据元信息 列表 */
defineOptions({ name: 'Databasesharing' })
const router = useRouter()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CodegenTableVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive<any>({
  pageNo: 1,
  pageSize: 10,
  tableName: undefined,
  tableComment: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const detailVisible = ref<boolean>(false)
const currentRow = ref<CodegenTableVO>()

/** 处理部门被点击 */
const handleDeptNodeClick = async (row) => {
  queryParams.dataSourceConfigId = row.id
  await getList()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CodegenApi.getCodegenTablePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
/** 导入操作 */
const importRef = ref()
const openImportTable = () => {
  importRef.value.open()
}
// 详情弹窗
const openDetail = (row: any) => {
  currentRow.value = { ...row }
  detailVisible.value = true
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CodegenApi.deleteCodegenTable(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const handleLook = (id) => {
  router.push({ path: '/system/databasesharingstandard', query: { id } })
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>